import Vue from 'vue'
import Vuex from 'vuex'
import { compatibleStorage } from './utils'
import buryPoint from './modules/buryPoint.js'
import request from "../ext/http.js"

Vue.use(Vuex)
// 用于指向内存地址，指向chatList
let test={}

// var myThis;
let store = new Vuex.Store({
	modules: {
		buryPoint
	},
	state: {
		token: compatibleStorage('token') || '', //登录后返回 //登录后返回
		sessionId: compatibleStorage('sessionId') || "", //登录后返回
		// sessionId:uni.getStorageSync('sessionId') || "",//登录后返回
		userInfoDetail: uni.getStorageSync('userInfoDetail') || {}, //登录后返回，用户信息
		unionID: compatibleStorage('unionID') || "",
		ua: '', // UserAgent
		headerHeight: 0,
		chatList: compatibleStorage('chatList') || [], //聊天列表
		chatObject: compatibleStorage('chatObject') || {} //聊天列表对象
	},
	mutations: {

		setLoginInfo(state, info) {
			info.token && (state.token = info.token)
			info.userInfoDetail && (state.userInfoDetail = info.userInfoDetail);
			// uni.setStorageSync('token',info.token);
			info.token && uni.setStorageSync('token',info.token);

			// uni.setStorageSync('sessionId',info.sessionId);
			info.userInfoDetail && uni.setStorageSync('userInfoDetail', info.userInfoDetail);
		},
		clearLoginInfo(state) {
			state.token = "";
			state.userInfoDetail = {};
			state.chatList = [];
			state.chatObject = {};
			uni.removeStorageSync('token');
			uni.removeStorageSync('userInfoDetail');
			uni.removeStorageSync('chatList');
			uni.removeStorageSync('chatObject');
		},

		setUserInfo(state, info) {
			state.userInfoDetail = info;
			uni.setStorageSync('userInfoDetail', info);
		},
		setSystemInfo(state, info) {
			state.systemInfo = info
		},
		setNetworkType(state, info) {
			if (info == 'unknown' || info == 'none') {
				info = '';
			}
			state.networkType = info || '';
		},
		headerHeight(state, info) {
			state.headerHeight = info
		},
		setToken(state, info) {
			state.token = info
		},
		setAppSystemInfo(state, info) {
			state.appSystemInfo = info;
		},
		setUserAgent(state, info) {
			state.ua = info;
		},
		setMenuButtonPosition(state, info){
			state.menuButtonPosition = info
		},
		setChatList(state, info) {
			state.chatList = info
			state.chatList.forEach((item) => {
				 state.chatObject[item.targetUserId] = item
					test[item.targetUserId] = item
			})
			info && uni.setStorageSync('chatList', state.chatList);
			info && uni.setStorageSync('chatObject',state.chatObject);
		},
		setOneChatObject(state, info) {
			if (!state.chatObject[info.id]) {
				// 列表添加一个新用户
				state.chatList = [
					...[{
						targetUserId: info.id,
						targetUserName: info.name,
						targetUserAvatar: info.avatar,
						myChatLogRespList: []
					}],
					...state.chatList
				]
				test[info.id] = state.chatList[0]
				state.chatObject[info.id] = state.chatList[0]
			}
		},
		setChatObject(state, { info, type }) {
			let id = type === 'on' ? info.fromUserId:info.targetUserId
			state.chatList.forEach((item) => {
					test[item.targetUserId] = item
			})
			let list = state.chatObject[id].myChatLogRespList
			state.chatObject[id].myChatLogRespList = [...[info], ...list]
			test[id].myChatLogRespList = [...[info], ...list]
			// state.chatObject[info.targetUserId].myChatLogRespList.push(info)
			// test[info.targetUserId].myChatLogRespList.push(info)
			info && uni.setStorageSync('chatList', state.chatList);
			info && uni.setStorageSync('chatObject',state.chatObject);
		}
	},
	getters: {
	},
	actions: {
	}
})

!Vue.prototype.$globalData && Object.defineProperties(Vue.prototype, {
	$globalData: {
		get() {
			store.myThis = this;
			return store
		}
	}
});

export default store
